#!/usr/bin/perl

package Fifo;
use Scheduler;
use Switch;
use strict;
our @ISA = qw(Scheduler);    

# Override constructor
sub new {
    my ($class) = @_;

    my $self = $class->SUPER::new( $_[1], $_[2], $_[3] );
  	  	
  	$self->{_ThreadQuantum} = $_[4];  	
  	$self->{_ProcessAlgorithm} = 'FIFO';
  	  	
    bless $self, $class;
    return $self;
}

sub doTick{
	my( $self ) = @_;
	
	my $currentProcess;
	
	#1er kernel
	if(!$self->{_executing}){
		my $proc;
		my $i=0;
		
		if(@{$self->{_ready}}>0){
			
			foreach $proc (@{$self->{_ready}}){
			
			if(@{$proc->getBursts()} || $proc->hasUlts()){
				$currentProcess = $proc;
				last;
			}			
			
			$i++;
			
		  }
		
		}
		
		if($currentProcess){
			
			splice @{$self->{_ready}}, $i, 1;
			
			if($currentProcess->hasUlts() && ($self->{_ultAlgorithmin} eq "SPN" || $self->{_ultAlgorithmin} eq "SRT")){
				$currentProcess->orderUltByMinor();
			}elsif($currentProcess->hasUlts() && $self->{_ultAlgorithmin} eq "HRRN"){
				$currentProcess->orderUltByPriorityHRRN($self->{_ticks});
			}
			
			$self->{_executing} = $currentProcess;
		}
		
	}	
	
	#2do Kernel	
	if($self->{_kernels} eq 2 && !$self->{_executing2}){
		$currentProcess=undef;
		
		my $proc;
		my $i=0;
		
		if(@{$self->{_ready}}>0){
			
			foreach $proc (@{$self->{_ready}}){
			
			if(@{$proc->getBursts()} || $proc->hasUlts()){
				$currentProcess = $proc;
				last;
			}			
			
			$i++;
			
		  }
		
		}
		
		if($currentProcess){
			
			splice @{$self->{_ready}}, $i, 1;
			
			if($currentProcess->hasUlts() && ($self->{_ultAlgorithmin} eq "SPN" || $self->{_ultAlgorithmin} eq "SRT")){
				$currentProcess->orderUltByMinor();
			}elsif($currentProcess->hasUlts() && $self->{_ultAlgorithmin} eq "HRRN"){
				$currentProcess->orderUltByPriorityHRRN($self->{_ticks});
			}
			
			$self->{_executing2} = $currentProcess;
		}
	}

}
1;
